package com.weds.edpf.core.service;

import com.weds.core.base.BaseService;
import com.weds.edpf.core.entity.AcDepEntity;
import com.weds.edpf.core.mapper.AcDepMapper;
import com.weds.edpf.core.util.CharacterUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author
 * @Description AcDep管理
 * @Date 2019-10-17
 */
@Service
public class AcDepService extends BaseService {

    @Resource
    private AcDepMapper acDepMapper;

    private Logger log = LogManager.getLogger();

    public int deleteByPrimaryKey(Integer depSerial) {
        return acDepMapper.deleteByPrimaryKey(depSerial);
    }

    public int insert(AcDepEntity record) {
        return acDepMapper.insert(record);
    }

    public int insertSelective(AcDepEntity record) {
        return acDepMapper.insertSelective(record);
    }

    public AcDepEntity selectByPrimaryKey(Integer depSerial) {
        return acDepMapper.selectByPrimaryKey(depSerial);
    }

    public int updateByPrimaryKeySelective(AcDepEntity record) {
        return acDepMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(AcDepEntity record) {
        return acDepMapper.updateByPrimaryKey(record);
    }

    public List<AcDepEntity> selectListByEntity(AcDepEntity record) {
        return acDepMapper.selectListByEntity(record);
    }

    public List<AcDepEntity> selectDinePlace() {
        return acDepMapper.selectDinePlace();
    }

    public int deleteBatchByKeys(List<AcDepEntity> list) {
        return acDepMapper.deleteBatchByKeys(list);
    }

    public List<AcDepEntity> selectAllDep() {
        return acDepMapper.selectAllDep();
    }

    public List<AcDepEntity> selectDepByParent(Integer depParent) {
        return acDepMapper.selectDepByParent(depParent);
    }

    public AcDepEntity selectDineHall(String devSbId) {
        return acDepMapper.selectDineHall(devSbId);
    }

    public List<AcDepEntity> moreThanOrder(Integer depParent, Integer depOrder) {
        return acDepMapper.moreThanOrder(depParent, depOrder);
    }


    public AcDepEntity selectSameName(Integer depParent, String depName) {
        return acDepMapper.selectSameName(depParent, depName);
    }

    public int countDepPlaceByParent( Integer depParent) {
        return acDepMapper.countDepPlaceByParent(depParent);
    }

    public List<AcDepEntity> selectListBetwweenOrder(Integer depParentNo, Integer order1, Integer order2) {
        return acDepMapper.selectListBetwweenOrder(depParentNo, order1, order2);
    }





    public void depPlaceReOrder(List<AcDepEntity> list2 , Integer depSerial , int upDown){
        if(list2 != null && list2.size() != 0){
            for (AcDepEntity acDepEntity : list2) {
                if(acDepEntity.getDepSerial().equals(depSerial)){
                    continue;
                }
                AcDepEntity entity1 = new AcDepEntity();
                int newDepOrder = acDepEntity.getDepOrder()+upDown;
                entity1.setDepOrder(newDepOrder);
                String d = CharacterUtil.DeciamlToThirtySix(newDepOrder);
                if(d.length() == 1){
                    d = "0" + d;
                }
                String  depNo = acDepEntity.getDepNo();
                String newDepNo = depNo.substring(0,depNo.length() -2) + d;
                entity1.setDepNo(newDepNo);
                entity1.setDepSerial(acDepEntity.getDepSerial());
                //同级相关部门 更新
                this.updateByPrimaryKeySelective(entity1);

                //子目录相应变更
                childDepPlaceUpdate(acDepEntity.getDepSerial(),newDepNo,0);
            }
        }

    }


    public void childDepPlaceUpdate(Integer depSerial, String newDepNo,int index){
        if(index > 30){
            return ;
        }
        //子目录相应变更  depNo = 新parent depNo + depOrder
        List<AcDepEntity>  childDep = this.selectDepByParent(depSerial);
        if(childDep != null && childDep.size() != 0){
            index++;
            for (AcDepEntity entity : childDep) {
                AcDepEntity entity2 = new AcDepEntity();
                Integer order = entity.getDepOrder();
                String d = CharacterUtil.DeciamlToThirtySix(order);
                if(d.length() == 1){
                    d = "0" + d;
                }
                entity2.setDepNo(newDepNo + d);
                entity2.setDepSerial(entity.getDepSerial());
                this.updateByPrimaryKeySelective(entity);

                childDepPlaceUpdate(entity.getDepSerial(),entity2.getDepNo(),index);
            }
        }

    }



}
